package com.cdp.zwy.zwy_manager_back_v1.service;

import cn.hutool.core.util.StrUtil;
import com.cdp.zwy.zwy_manager_back_v1.common.ServiceException;
import com.cdp.zwy.zwy_manager_back_v1.common.ErrorCode;
import com.cdp.zwy.zwy_manager_back_v1.entity.Account;
import com.cdp.zwy.zwy_manager_back_v1.entity.Employee;
import com.cdp.zwy.zwy_manager_back_v1.mapper.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.Data;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/4/5 12:35
 */
@Service
public class EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;
    //当add被调用redis会清楚employeePage中数据
    @CacheEvict(value = "employeePage",allEntries = true)
    public int add(Employee employee) {
        if(StrUtil.isBlank(employee.getName())){
            employee.setName("默认名称");
        }
        if(StrUtil.isBlank(employee.getRole())){
            employee.setRole("EMP");
        }
        int row =  employeeMapper.insert(employee);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_ADD_ERROR);
        }
        return row;
    }
    @Caching(evict = {
            @CacheEvict(value = "employeePage", allEntries = true),
            @CacheEvict(value = "employee", key = "#employee.id")
    })
    public int deleteById(Integer id) {
         Employee employee = employeeMapper.selectById(id);
         if (employee == null){
             throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
         }
        int row =  employeeMapper.deleteById(id);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_DELETE_ERROR);
        }
        return row;
    }
    @Caching(evict = {
            @CacheEvict(value = "employeePage", allEntries = true),
            @CacheEvict(value = "employee", key = "#employee.id")
    })
    public int modify(Employee employee) {
        Employee employee1 = employeeMapper.selectById(employee.getId());
        if (employee1 == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        int row =  employeeMapper.update(employee);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_MODIFY_ERROR);
        }
        return row;
    }
    @Cacheable(value = "employee", key = "#employee.id")
    public Employee findById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        if(employee == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        return employee;
    }
    @CachePut(value = "employee", key = "#employee.id")
    public List<Employee> findAll() {
        List<Employee> employees = employeeMapper.selectAll();
        if(employees.size() == 0){
            throw new ServiceException(ErrorCode.NOT_DATA);
        }
        return employees;
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Cacheable(value = "employeePage", key = "#pageNum + '-' + #pageSize")
    public PageInfo<Employee> selectPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> list = employeeMapper.selectAll();

        return PageInfo.of(list);
    }
    @Cacheable(value = "employeePage", key = "#pageNum + '-' + #pageSize")
    public PageInfo<Employee> findByName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> list = employeeMapper.selectFuzzy(name);

        return PageInfo.of(list);
    }
    @Caching(evict = {
            @CacheEvict(value = "employeePage", allEntries = true),
    })
    public int deleteBatch(List<Integer> ids) {
        int rows = 0;
        for (Integer id : ids) {
            Employee employee = employeeMapper.selectById(id);
            if (employee == null){
                throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
            }
            int row =  employeeMapper.deleteById(id);
            if(row!=1){
                throw new ServiceException(ErrorCode.EMPLOYEE_DELETE_ERROR);
            }
            rows++;
        }
      return rows;
    }

    public Employee login(Account employee) {
        Employee findEmployee = employeeMapper.selectByUsername(employee.getUsername());
        if (findEmployee == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        if (!findEmployee.getPassword().equals(employee.getPassword())){
            throw new ServiceException(ErrorCode.EMPLOYEE_LOGIN_ERROR);
        }
        return findEmployee;
    }
    public Employee register(Employee employee){
        Employee findEmployee = employeeMapper.selectByUsername(employee.getUsername());
            employee.setRole("EMP");
            employee.setName("默认名称");
            employee.setGender("男");
            employee.setTitle("普通员工");
            employee.setBirthday(new Date());


        if (findEmployee != null){
            throw new ServiceException(ErrorCode.EMPLOYEE_REGISTER_ERROR_USERNAME);
        }
        int row =  employeeMapper.insert(employee);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_ADD_ERROR);
        }
        return employee;
    }
    public Employee modifyPassword(Account employee) {
        Employee findEmployee = employeeMapper.selectById(employee.getId());
        if (findEmployee == null){
            throw new ServiceException(ErrorCode.EMPLOYEE_NOT_FOUND);
        }
        if (!findEmployee.getPassword().equals(employee.getOldPassword())){
            throw new ServiceException(ErrorCode.EMPLOYEE_OLDPASSWORD_ERROR);
        }
        findEmployee.setPassword(employee.getPassword());
        int row =  employeeMapper.update(findEmployee);
        if(row!=1){
            throw new ServiceException(ErrorCode.EMPLOYEE_MODIFY_ERROR);
        }
        return findEmployee;
    }

}
